﻿using RuneMasterWin8.Core;
using RuneMasterWin8.Util;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Graphics.Display;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// “基本页”项模板在 http://go.microsoft.com/fwlink/?LinkId=234237 上有介绍

namespace RuneMasterWin8.Pages
{
    /// <summary>
    /// 基本页，提供大多数应用程序通用的特性。
    /// </summary>
    public sealed partial class StagePage : RuneMasterWin8.Common.LayoutAwarePage
    {
        public StagePage()
        {
            this.InitializeComponent();
            DisplayProperties.AutoRotationPreferences = DisplayOrientations.Landscape | DisplayOrientations.LandscapeFlipped;
        }

        /// <summary>
        /// 使用在导航过程中传递的内容填充页。在从以前的会话
        /// 重新创建页时，也会提供任何已保存状态。
        /// </summary>
        /// <param name="navigationParameter">最初请求此页时传递给
        /// <see cref="Frame.Navigate(Type, Object)"/> 的参数值。
        /// </param>
        /// <param name="pageState">此页在以前会话期间保留的状态
        /// 字典。首次访问页面时为 null。</param>
        protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
        {
            string gameMode = StageManager.Instance.CurrentGameMode;

            //更新界面显示
            if (StageManager.Instance.CurrentGameMode == "e")
            {
                this.imgGameModeEasy.Visibility = Windows.UI.Xaml.Visibility.Visible;
                this.imgGameModeHard.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                this.imgGameModeNormal.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
            }
            else if (StageManager.Instance.CurrentGameMode == "n")
            {
                this.imgGameModeEasy.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                this.imgGameModeHard.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                this.imgGameModeNormal.Visibility = Windows.UI.Xaml.Visibility.Visible;
            }
            else if (StageManager.Instance.CurrentGameMode == "h")
            {
                this.imgGameModeEasy.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                this.imgGameModeHard.Visibility = Windows.UI.Xaml.Visibility.Visible;
                this.imgGameModeNormal.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
            }

            int stageUnlockCount = 0;

            //获取该Mode下的关卡完成情况
            if (StageManager.Instance.StageUnlockStatus.ContainsKey(gameMode + "_" + 1)
                && StageManager.Instance.StageUnlockStatus[gameMode + "_" + 1])
            {
                stageUnlockCount++;
                btnStage1.IsEnabled = true;
            }
            else
            {
                btnStage1.IsEnabled = false;
            }

            if (StageManager.Instance.StageUnlockStatus.ContainsKey(gameMode + "_" + 2)
    && StageManager.Instance.StageUnlockStatus[gameMode + "_" + 2])
            {
                stageUnlockCount++;
                btnStage2.IsEnabled = true;
            }
            else
            {
                btnStage2.IsEnabled = false;
            }

            if (StageManager.Instance.StageUnlockStatus.ContainsKey(gameMode + "_" + 3)
&& StageManager.Instance.StageUnlockStatus[gameMode + "_" + 3])
            {
                stageUnlockCount++;
                btnStage3.IsEnabled = true;
            }
            else
            {
                btnStage3.IsEnabled = false;
            }

            if (StageManager.Instance.StageUnlockStatus.ContainsKey(gameMode + "_" + 4)
&& StageManager.Instance.StageUnlockStatus[gameMode + "_" + 4])
            {
                stageUnlockCount++;
                btnStage4.IsEnabled = true;
            }
            else
            {
                btnStage4.IsEnabled = false;
            }

            if (StageManager.Instance.StageUnlockStatus.ContainsKey(gameMode + "_" + 5)
&& StageManager.Instance.StageUnlockStatus[gameMode + "_" + 5])
            {
                stageUnlockCount++;
                btnStage5.IsEnabled = true;
            }
            else
            {
                btnStage5.IsEnabled = false;
            }

            if (StageManager.Instance.StageUnlockStatus.ContainsKey(gameMode + "_" + 6)
&& StageManager.Instance.StageUnlockStatus[gameMode + "_" + 6])
            {
                stageUnlockCount++;
                btnStage6.IsEnabled = true;
            }
            else
            {
                btnStage6.IsEnabled = false;
            }

            if (StageManager.Instance.StageUnlockStatus.ContainsKey(gameMode + "_" + 7)
&& StageManager.Instance.StageUnlockStatus[gameMode + "_" + 7])
            {
                stageUnlockCount++;
                btnStage7.IsEnabled = true;
            }
            else
            {
                btnStage7.IsEnabled = false;
            }

            if (StageManager.Instance.StageUnlockStatus.ContainsKey(gameMode + "_" + 8)
&& StageManager.Instance.StageUnlockStatus[gameMode + "_" + 8])
            {
                stageUnlockCount++;
                btnStage8.IsEnabled = true;
            }
            else
            {
                btnStage8.IsEnabled = false;
            }

            if (StageManager.Instance.StageUnlockStatus.ContainsKey(gameMode + "_" + 9)
&& StageManager.Instance.StageUnlockStatus[gameMode + "_" + 9])
            {
                stageUnlockCount++;
                btnStage9.IsEnabled = true;
            }
            else
            {
                btnStage9.IsEnabled = false;
            }

            if (StageManager.Instance.StageUnlockStatus.ContainsKey(gameMode + "_" + 10)
&& StageManager.Instance.StageUnlockStatus[gameMode + "_" + 10])
            {
                stageUnlockCount++;
                btnStage10.IsEnabled = true;
            }
            else
            {
                btnStage10.IsEnabled = false;
            }
        }

        private void btnStage_Click_1(object sender, RoutedEventArgs e)
        {
            AudioPlayHelper.PlayEffectSound("button_click");
            Button btn = sender as Button;
            if (btn != null)
            {
                string stageIndex = btn.Tag.ToString();
                StageManager.Instance.CurrentStageIndex = stageIndex;
                //跳转到道具选择页面
                this.Frame.Navigate(typeof(ToolChoosePage));
            }
        }

        /// <summary>
        /// 保留与此页关联的状态，以防挂起应用程序或
        /// 从导航缓存中放弃此页。值必须符合
        /// <see cref="SuspensionManager.SessionState"/> 的序列化要求。
        /// </summary>
        /// <param name="pageState">要使用可序列化状态填充的空字典。</param>
        protected override void SaveState(Dictionary<String, Object> pageState)
        {
        }
    }
}
